package com.aaa.controller.front;

import com.aaa.entity.FrontUserinfo;

import com.aaa.entity.ShippingAddress;
import com.aaa.entity.ShoppingCart;
import com.aaa.entity.ShoppingCartDetail;
import com.aaa.service.front.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/front")
@Controller
public class AccountController {

    @Resource
    private OrderService orderService;
    @Resource
    private IFrontUserinfoService frontUserinfoService;
    @Resource
    private IFrontUserAddressService frontUserAddressService;
    @Resource
    private IShoppingCartDetailService shoppingCartDetailService;
    @Resource
    private IShoppingCartService shoppingCartService;


    @RequestMapping("/logout")
    public String logout() {

        return "front/logout";
    }

    @RequestMapping("/logout_submit")
    public String logout_submit(Model model, String frontUserEmail, String frontUserPwd, HttpServletRequest request) {
        FrontUserinfo userinfo = frontUserinfoService.login(frontUserEmail, frontUserPwd);
        HttpSession session = request.getSession();
        session.setAttribute("userinfo",userinfo);
        if(userinfo==null){
            model.addAttribute("err","邮箱或密码为空");
            return "front/logout";
        }
        if(userinfo.getFrontUserState()==1){
            session.setAttribute("err","用户已被禁用");
            return "front/logout";
        }else{
            session.setAttribute("FrontUserinfo",userinfo);
            return "redirect:/front/index";
        }

    }
    //用户注销
    @RequestMapping("/login_out")
    public String login_out(HttpSession session){
        session.removeAttribute("FrontUserinfo");
        return "/front/index";
    }
    //跳到注册页面
    @RequestMapping("/register_show")
    public String register_show(){
        return "/front/logout";
    }
    @RequestMapping("/register_submit")
    public String register_submit(FrontUserinfo frontUserinfo){
        frontUserinfoService.insertByUser(frontUserinfo);
        return "redirect:/front/logout";
    }
    //校验用户名是否存在
    @RequestMapping("/checkAccountEmail")
    @ResponseBody
    public Map<String,Object> checkAccountEmail(String frontUserEmail){
        boolean isExists=frontUserinfoService.isNameExists(frontUserEmail);
        Map<String,Object>map= new HashMap<String,Object>();
        map.put("is_frontUserEmaile_exists",isExists);
        return map;
    }
    //个人资料
    @RequestMapping("/myaccount")
    public String myaccount(Model model,Integer UserInfoId,HttpSession session){
        FrontUserinfo frontUserinfo=frontUserinfoService.findByUserInfoId(UserInfoId);
        model.addAttribute("frontUserinfo",frontUserinfo);

        List<Map<String, Object>> salesList = orderService.findSales();
        model.addAttribute("salesList",salesList);
        return "/front/myaccount";
    }


    @RequestMapping("/myaccount_update")
    public String myaccount_update(FrontUserinfo frontUserinfo){
        frontUserinfoService.update(frontUserinfo);
        System.out.println(frontUserinfo);
        return "redirect:/front/myaccount";
    }

    @RequestMapping("/checkOut")
    public String checkOut(Model model, Integer UserInfoId, HttpSession session){
        FrontUserinfo f=(FrontUserinfo) session.getAttribute("FrontUserinfo");
        UserInfoId=f.getUserInfoId();
        List<ShippingAddress> addressList1=frontUserAddressService.listByAddress1(UserInfoId);
        model.addAttribute("addressList1",addressList1);

        FrontUserinfo userinfo = (FrontUserinfo) session.getAttribute("userinfo");
        Integer UserInfoId_1 = userinfo.getUserInfoId();
        ShoppingCart cart = shoppingCartService.findCartUserInfoId(UserInfoId);
        List<ShoppingCartDetail> userInfoCartDetail = shoppingCartDetailService.findShoppingCartDetail(cart.getShoppingCartId());
        int totalCount=0;
        double totalAmount=0;
        for (ShoppingCartDetail g:userInfoCartDetail){
            totalCount +=  g.getGoodsCount();
            totalAmount+=g.getGoodsAmount()*g.getGoodsCount();
        }
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalAmount",totalAmount);
        model.addAttribute("userInfoCartDetail",userInfoCartDetail);


        return "front/checkout";
    }

}



